<?PHP
if (is_array($entity)) {
    foreach ($entity as $entity_item) {
        ?>
        <input name="ajax_id" id="ajax_id" type="hidden" value="<?PHP echo $entity_item['cu_id']; ?>"/>
        <?php
    }
}
?>
<script src="<?php echo base_url(); ?>script/My97DatePicker/WdatePicker.js" type="text/javascript"></script>
<script id="scriptInit" type="text/javascript">
    $(function () {
        oTable=$('#ListTable').dataTable({
            "bPaginate":false,
            "bLengthChange": false, 
            "bFilter":false,
            "bInfo":false,
            "oLanguage": {//语言国际化
                "sUrl": "script/DataTable/zh.text"
            }
        });
        //初始化Grid数据
        $.ajax({
            url:"<?php echo site_url(); ?>/customers/C_Customers_Edit/ajax_GetProduct/" + $("#ajax_id").val(),
            cache:false,
            success:function (result) {
                if (result != '') {
                    var dataObj = eval("(" + result + ")");
                    ;
                    $.each(dataObj, function (i, item) {
                        //填入grid
                        $('#ListTable').dataTable().fnAddData([
                            item.pt_id,
                            item.pt_name,
                            item.pt_num,
                            "<a href='javascript:void(0);' onclick='deleteTable(this)'>删除</a>" ]
                    );
                    })
                }
            }
        });
    });
</script>
<div class="right01"><img src="content/images/04.gif"/> <a href="<?php echo site_url(); ?>/C_Main/index/">主页</a> &gt;
    <span>订单信息列表</span> &gt; <span>操作订单信息</span>
</div>
<div id="content">
    <div id="tabsCount">
        <?php
        $attributes = array('id' => 'submitform');
        echo form_open('customers/C_Customers_Edit/edit_Customers', $attributes);
        ?>
        <table class="tables">
            <?PHP
            if (is_array($entity)) {
                foreach ($entity as $entity_item) {
                    ?>
                    <tr>
                        <td style=" width: 19%;">&nbsp;</td>
                        <td style=" width: 12%;">新郎：</td>
                        <td style=" width: 19%;"><input id="cu_groom" name="cu_groom" type="text"
                                                        value="<?PHP echo $entity_item['cu_groom']; ?>"/><span
                                                        style=" color: red">*</span></td>
                        <td style=" width: 12%;">新娘：</td>
                        <td style=" width: 19%;"><input id="cu_bride" name="cu_bride" type="text"
                                                        value="<?PHP echo $entity_item['cu_bride']; ?>"/><span
                                                        style=" color: red">*</span></td>
                        <td style=" width: 19%;">&nbsp;</td>
                    </tr>
                    <tr>
                        <td style=" width: 19%;">&nbsp;</td>
                        <td style=" width: 12%;">手机：</td>
                        <td style=" width: 19%;"><input id="cu_cellphone" name="cu_cellphone" type="text"
                                                        value="<?PHP echo $entity_item['cu_cellphone']; ?>"/><span
                                                        style=" color: red">*</span></td>
                        <td style=" width: 12%;">固定电话：</td>
                        <td style=" width: 19%;"><input id="cu_phone" name="cu_phone" type="text"
                                                        value="<?PHP echo $entity_item['cu_phone']; ?>"/></td>
                        <td style=" width: 19%;">&nbsp;</td>
                    </tr>
                    <tr>
                        <td style=" width: 19%;">&nbsp;</td>
                        <td style=" width: 12%;">结婚日期：</td>
                        <td style=" width: 19%;"><input id="cu_marrydate" name="cu_marrydate" type="text"
                                                        onClick="WdatePicker()"
                                                        value="<?PHP echo $entity_item['cu_marrydate'] != '' ? $entity_item['cu_marrydate'] : date("Y-m-d"); ?>"/>
                        </td>
                        <td style=" width: 12%;">取件日期：</td>
                        <td style=" width: 19%;"><input id="cu_takedate" name="cu_takedate" type="text"
                                                        onClick="WdatePicker()"
                                                        value="<?PHP echo $entity_item['cu_takedate'] != '' ? $entity_item['cu_takedate'] : date("Y-m-d"); ?>"/>
                        </td>
                        <td style=" width: 19%;">&nbsp;</td>
                    </tr>
                    <tr>
                        <td style=" width: 19%;">&nbsp;</td>
                        <td style=" width: 12%;">地址：</td>
                        <td colspan="3">
                            <input id="cu_address" name="cu_address" style="width: 95%;" type="text"
                                   value="<?PHP echo $entity_item['cu_address']; ?>"/>
                        </td>
                        <td style=" width: 19%;">&nbsp;</td>
                    </tr>
                    <tr>
                        <td style=" width: 19%;">&nbsp;</td>
                        <td style=" width: 12%;">套系：</td>
                        <td style=" width: 19%;">
                            <select id="select1" style=" width: 65%">
                                <option>请选择</option>
                                <?php
                                if (is_array($selectList)) {
                                    foreach ($selectList as $list_item) {
                                        if ($list_item['se_id'] == $entity_item['se_id']) {
                                            ?>
                                            <option value="<?PHP echo $list_item['se_id']; ?>"
                                                    selected="selected"><?PHP echo $list_item['se_name']; ?></option>
                                                    <?php
                                                } else {
                                                    ?>
                                            <option value="<?PHP echo $list_item['se_id']; ?>"><?PHP echo $list_item['se_name']; ?></option>
                                            <?php
                                        }
                                    }
                                }
                                ?>
                            </select><span style=" color: red">*</span>
                        </td>
                        <td style=" width: 12%;">金额总计：</td>
                        <td style=" width: 19%;"><input id="cu_total" name="cu_total" type="text" readonly
                                                        value="<?PHP echo $entity_item['cu_total']; ?>"/></td>
                        <td style=" width: 19%;">&nbsp;</td>
                    </tr>
                    <tr>
                        <td style=" width: 19%;">&nbsp;</td>
                        <td style=" width: 12%;">已付金额：</td>
                        <td style=" width: 19%;"><input id="cu_paid" name="cu_paid" type="text"
                                                        value="<?PHP echo $entity_item['cu_paid']; ?>"/></td>
                        <td style=" width: 12%;">未付金额：</td>
                        <td style=" width: 19%;"><input id="cu_unpaid" name="cu_unpaid" readonly type="text"
                                                        value="<?PHP echo $entity_item['cu_unpaid']; ?>"/></td>
                        <td style=" width: 19%;">&nbsp;</td>
                    </tr>
                    <tr>
                        <td style=" width: 19%;">&nbsp;</td>
                        <td style=" width: 12%;">套系内容：</td>
                        <td style=" width: 19%;">&nbsp;</td>
                        <td style=" width: 19%;">&nbsp;</td>
                        <td style="text-align: right"><span style="cursor:pointer" id="add_button">[添加]</span> &nbsp;
                            &nbsp;</td>
                        <td style=" width: 19%;">&nbsp;</td>
                    </tr>
                    <tr>
                        <td colspan="6">
                            <table id="ListTable" class="tables">
                                <thead>
                                    <tr>
                                        <th>
                                            商品编号
                                        </th>
                                        <th>
                                            商品名称
                                        </th>
                                        <th>
                                            数量
                                        </th>
                                        <th>
                                            操作
                                        </th>
                                    </tr>
                                </thead>
                                <tbody>

                                </tbody>
                            </table>
                        </td>
                    </tr>
                    <input name="cu_id" id="cu_id" type="hidden" value="<?PHP echo $entity_item['cu_id']; ?>"/>
                    <input name="se_item" id="se_item" type="hidden"/>
                    <input name="se_id_select" id="se_id_select" type="hidden"
                           value="<?PHP echo $entity_item['se_id']; ?>"/>
                    <input name="cu_print" id="cu_print" type="hidden" value="0"/><!--确定是否需要打印-->
                    <?PHP
                }
            }
            ?>
        </table>
        <div style="margin-top: 20%; margin-left: 35%;">
            <input class="button" type="submit" name="postsubmit" value="提交"/>
            <input class="button" type="button" name="resetbtn" onClick="resetButton()" value="返回"/>
        </div>
    </div>
</form>

<div id="show_dialog" title="选择商品" style="display:none;">
    <table style="width: 100%">
        <tr>
            <td>商品名称</td>
            <td><select name='product_name' id='product_name'>
                    <?PHP
                    if (is_array($productlist)) {
                        foreach ($productlist as $product_item) {
                            ?>
                            <option value="<?PHP echo $product_item['pt_id']; ?>"><?PHP echo $product_item['pt_name']; ?></option>
                            <?PHP
                        }
                    }
                    ?>
                </select></td>
            <td>
                数量
            </td>
            <td><input id="product_number" name="product_number" value="1" type="text" style="width:100px;"/></td>
        </tr>
    </table>
</div>
</div>
<div style="display: none">
    <object  id="LODOP_OB" classid="clsid:2105C259-1E0C-4534-8141-A753534CB4CA" width=0 height=0> 
        <embed id="LODOP_EM" type="application/x-print-lodop" width=0 height=0></embed>
    </object>
</div>
<!--打印控件-->
<script src="<?php echo base_url(); ?>script/lodopPrint/LodopFuncs.js" type="text/javascript"></script>
<script src="<?php echo base_url(); ?>script/lodopPrint/LodopConfiguration.js" type="text/javascript"></script>
<script type="text/javascript">
    //提交插入信息时，获取表格全部信息
    $("#submitform").submit(function (e) {
        //判断信息的完整性
        if ($("#cu_groom").val() == "" || $("#cu_bride").val() == "" || $("#cu_cellphone").val() == "") {
            $.prompt("<span style='color:#FF0000'>请确认信息填写完整！</span>", {
                title:"提示",
                buttons:{ "确定":true},
                timeout:5000
            });
            return false;
        } else {
            if (isNaN($("#cu_cellphone").val())) {
                $.prompt("<span style='color:#FF0000'>手机号码只能填写数字！</span>", {
                    title:"提示",
                    buttons:{ "确定":true},
                    timeout:5000
                });
                return false;
            }
        }

        var tableTr = $('#ListTable tr');
        var resultStr = "";
        var index = 0;
        $("td", tableTr).each(function () {
            if ($(this).text() != "没有检索到数据") {
                if ($(this).text() != "删除") {
                    resultStr += $(this).text() + ",";
                } else {
                    resultStr += ";";
                }
                index++;
            }
        });
        if (index == 0) {
            $.prompt("<span style='color:#FF0000'>亲，请选择需要的商品哦！</span>", {
                title:"提示",
                buttons:{ "确定":true},
                timeout:5000
            });
            return false;
        }
        $("#se_item").val(resultStr);
        if ($("#cu_id").val() == "") {
            if(confirm('是否需要打印？')){
                $("#ListTable tr :last-child").remove();  
                var tableHtml="<table style=' width: 100%;font-size: 9px;text-align:center'>"+$("#ListTable").html()+"</table>";
                //新郎姓名，新娘姓名，套系名称，未付金额，操作人ID，订单编号，客户电话，总计金额，套系内容
                printLod();
                var myDate = new Date();
                LODOP.ADD_PRINT_TEXT(126,150,100,20,$("#cu_groom").val());
                LODOP.ADD_PRINT_TEXT(125,584,99,20,$("#cu_bride").val());
                LODOP.ADD_PRINT_TEXT(151,150,100,20,$("#select1").children('option:selected').text());
                LODOP.ADD_PRINT_TEXT(465,599,70,20,$("#cu_unpaid").val());
                LODOP.ADD_PRINT_TEXT(496,408,59,20,<?PHP echo $this->session->userdata('uid'); ?>);
                LODOP.ADD_PRINT_TEXT(97,537,150,20,myDate.toLocaleDateString());
                LODOP.ADD_PRINT_TEXT(152,584,103,20,$("#cu_cellphone").val());
                LODOP.ADD_PRINT_TEXT(496,572,112,20,"-星光影楼");
                LODOP.ADD_PRINT_TEXT(465,159,70,20,$("#cu_total").val());
                LODOP.ADD_PRINT_TABLE(177,52,633,265,tableHtml);
                LODOP.PREVIEW();
            };
        }
    });
    //返回
    function resetButton() {
        window.location.href = "<?php echo site_url(); ?>/customers/C_Customers_List/";
    }

    //下拉菜单选择后异步获取内容
    $('#select1').change(function () {
        $("#se_id_select").val($(this).children('option:selected').val());
        var oTable = $('#ListTable').dataTable();
        oTable.fnClearTable();
        $.ajax({
            url:"<?php echo site_url(); ?>/customers/C_Customers_Edit/ajax_Series/" + $(this).children('option:selected').val(),
            cache:false,
            success:function (result) {
                if (result != '') {
                    var arr = [];
                    var dataObj = eval("(" + result + ")");
                    $('#cu_total').val(dataObj[0].se_moeny);
                    $("#cu_unpaid").val(dataObj[0].se_moeny);
                    $.each(dataObj, function (i, item) {
                        //填入grid
                        $('#ListTable').dataTable().fnAddData([
                            item.pt_id,
                            item.pt_name,
                            item.pt_num,
                            "<a href='javascript:void(0);' onclick='deleteTable(this)'>删除</a>" ]
                    );
                    })
                }
            }
        });
    });

    //DIV添加山出框
    var statesdemo = {
        state0:{
            title:'选择商品',
            html:$("#show_dialog").html(),
            buttons:{ 确定:1 },
            focus:1,
            submit:function (e, v, m, f) {
                if (f.product_number == "") {
                    $("#product_number").css("border", "1px solid red");
                    return false;
                }
                //填入grid
                $('#ListTable').dataTable().fnAddData([
                    f.product_name,
                    m.find("option:selected").text(),
                    f.product_number,
                    "<a href='javascript:void(0);' onclick='deleteTable(this)'>删除</a>" ]
            );
                //清空文本内容
                $("#show_dialog input[type=text]").val("");
            }
        }
    };
    $("#add_button").click(function () {
        $.prompt(statesdemo);
    })

    //删除table中的一行
    function deleteTable(vals) {
        var row = $(vals).closest("tr").get(0);
        oTable.fnDeleteRow(oTable.fnGetPosition(row));
    }

    $(function () {
        $("#cu_paid").keyup(function () {
            var number = $("#cu_total").val() - $("#cu_paid").val();
            if (number < 0) {
                $.prompt("<span style='color:#FF0000'>“已付款”超出“总计”！</span>", {
                    title:"提示",
                    buttons:{ "确定":true},
                    timeout:5000
                });
                $("#cu_paid").val("");
                $("#cu_unpaid").val($("#cu_total").val());
            } else {
                $("#cu_unpaid").val(number);
            }
        })
    })
</script>